package org.practice.action;

import javax.servlet.http.HttpSession;

import org.practice.dao.UserDao;
import org.practice.model.User;
import org.practice.util.Constant;
import org.practice.util.CookieUtil;
import org.practice.util.ShopDaoFactory;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction {
	private static final String VERIFY="verify";
	private String email;
	private String password;
	private String refer;
	public String execute()throws Exception {
		//访问DAO
		UserDao dao=ShopDaoFactory.createUserDao();
		User user=dao.validateUser(email, password);
		if(user==null){
			this.addActionMessage(getText("error.verify.user"));
			return FAIL;
		}else if(user.isVerify()){
			//user对象通过email验证之后，可以将user对象的信息缓存在session，并且把user对象的id信息通过cookie保存在Client Browser
			HttpSession session =getSession();
			session.setAttribute(Constant.SESSION_KEY_USER,user);
			CookieUtil.addCookie(getResponse(), Constant.COOKIE_KEY_USERID, String.valueOf(user.getId()));
			return SUCCESS;
		}else{
			return VERIFY;
		}
		
	}
	
	public String loginForm()throws Exception {
		String url=getRequest().getHeader("Referer");
		refer = url.substring(url.lastIndexOf("/")+1);
		return SUCCESS;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getRefer() {
		return refer;
	}

	public void setRefer(String refer) {
		this.refer = refer;
	}
	
}
